// 1. typeof
function printID(id: string | number) {
  if(typeof id === 'string') {
    console.log(id.toLowerCase())
  } else {
    console.log(id.toFixed(2))
  }
}

// ===
type Direction = 'left' | 'right' | 'up' | 'down'
function switchDirection(direction: Direction) {
  if(direction === 'left') {
    console.log('左', direction)
  } else if(direction === 'right') {
    console.log('右', direction)
  } else if(direction === 'up') {
    console.log('上', direction)
  } else {
    console.log('下', direction)
  }
}

// instanceOf: 实例与对象
function printDate(date: string | Date) {
  if(date instanceof Date) {
    console.log(date.getTime())
  } else {
    console.log(date)
  }
}

// in：判断对象中是否有某一个属性
interface ISwim {
  swim: () => void
}
interface IRun {
  run: () => void
}

function move(animal: ISwim | IRun) {
  if('swim' in animal) {
    animal.swim()
  } else if('run' in animal) {
    animal.run()
  }
}

const fish: ISwim = {
  swim() {
    console.log('swim')
  }
}

const dog: IRun = {
  run() {
    console.log('run')
  }
}
move(fish)
move(dog)

export {}